package com.alliky.core.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.alliky.common.api.BaseApi;
import com.alliky.core.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

public class ImageLoader {

    private static String getImageUrl(String path) {
        if (!TextUtils.isEmpty(path) && !path.contains("http://") && !path.contains("https://")) {
            return BaseApi.BASE_IMAGE + path;
        } else {
            return path;
        }
    }

    public static void loadImage(Context context, String url, ImageView imageView) {
        loadImage(context, url, imageView, R.mipmap.ic_launcher, 2);
    }

    public static void loadImage(Context context, String url, ImageView imageView, @DrawableRes int resId) {
        loadImage(context, url, imageView, resId, 2);
    }

    public static void loadImage(Context context, String url, ImageView imageView, @DrawableRes int resId, int dp) {
        Log.d("print", "loadImage加载图片地址为====》" + url);
        Glide.with(context).load(getImageUrl(url))
                .placeholder(resId)//加载过程中显示图片
                .error(resId)//加载失败是显示图片
                .apply(new RequestOptions()
                        .dontAnimate()
                        .transform(new GlideRoundTransform(context, (int) ScreenUtils.px2Dp(context, dp))))
                .into(imageView);
    }
}
